package test.multi_thread;

import java.lang.management.ManagementFactory;
import java.util.concurrent.TimeUnit;

import log.debug.LogInterface;
import log.debug.LogTool;

/**
 * P662  21.2.8
 * @author yw
 *
 */
public class SimpleDaemons implements Runnable{
	private static final LogInterface mLog = new LogTool().getLogType();
	private static final String TAG = "SimpleDaemons";
	
	public String status() {
		String info = "  ";
		info += "pidInfo==";
		info += ManagementFactory.getRuntimeMXBean().getName();
		info += "\t thread==";
		info += Thread.currentThread().getName();
		
		return "" + this +"\t" + info;
	}


	public void run() {
			try {
				while (true) {
					TimeUnit.MILLISECONDS.sleep(100);
					mLog.i(TAG, status());
				}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
	}
	

	public static void main(String[] args) throws InterruptedException {
		// TODO Auto-generated method stub
		String info = "  ";
		info += "pidInfo==";
		info += ManagementFactory.getRuntimeMXBean().getName();
		info += "\t thread==";
		info += Thread.currentThread().getName();
		
		mLog.i(TAG, "main  start "  + info);
		for (int i=0; i<5; i++){
			Thread daemon = new Thread(new SimpleDaemons());
			daemon.setDaemon(true);  //Must call befor start
			daemon.start();
		}
		mLog.i(TAG, "main  middle "  + info);
		TimeUnit.MILLISECONDS.sleep(1000);
		mLog.i(TAG, "main  end "  + info);
	}

}
